<?php

$this_dir = dirname(__FILE__);
$ds = DIRECTORY_SEPARATOR;

$text = isset($_REQUEST['text']) ? true : false;

$grammar_fn = 'grammar';
if (!empty($_REQUEST['file'])) {
    $grammar_fn = urldecode($_REQUEST['file']);
}

$xml = file_get_contents($this_dir.$ds.$grammar_fn.'.xml');
$xsl = file_get_contents($this_dir.$ds.'schema2parser.xsl');

$xslt = new XSLTProcessor();

$xmlDOM = DOMDocument::loadXML($xml);
//$xmlDOM->schemaValidate($this_dir.$ds.'grammar.xsd');

$xslt->importStyleSheet(DOMDocument::loadXML($xsl));

$code = $xslt->transformToXML(DOMDocument::loadXML($xml));
$xsl2 = file_get_contents($this_dir.$ds.'xml2text.xsl');
$xslt->importStyleSheet(DOMDocument::loadXML($xsl2));

$res = $xslt->transformToDoc(DOMDocument::loadXML($code))->firstChild->nodeValue;

if ($text) {
    echo $res;
} else {
    highlight_string("<?php\n\n".$res."\n?>");
}

?>